import { entity } from "./table";
import * as Table from "./table";

const textSize = [
  6, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72,
];

export let MergeCells = () => {
  let range = GetSelectRange();
  if (!range) {
    return;
  }

  for (let i = range.startRow; i <= range.endRow; i++) {
    for (let j = range.startCol; j <= range.endCol; j++) {
      if (i == range.startRow && j == range.startCol) {
        continue;
      }
      entity.cells.value[i][j].show = false;
    }
  }

  let height = 0;
  for (let i = range.startRow; i <= range.endRow; i++) {
    height += entity.rowIndex.value[i + 1].height;
  }
  let width = 0;
  for (let i = range.startCol; i <= range.endCol; i++) {
    width += entity.colIndex.value[i + 1].width;
  }
  entity.cells.value[range.startRow][range.startCol].rowspan =
    range.endRow - range.startRow;
  entity.cells.value[range.startRow][range.startCol].colspan =
    range.endCol - range.startCol;
  entity.cells.value[range.startRow][range.startCol].width = width;
  entity.cells.value[range.startRow][range.startCol].height = height;
};

export let UnmergeCells = () => {
  if (!Table.startCell) {
    return;
  }
  for (
    let i = Table.startCell.row;
    i <= Table.startCell.row + Table.startCell.rowspan;
    i++
  ) {
    for (
      let j = Table.startCell.col;
      j <= Table.startCell.col + Table.startCell.colspan;
      j++
    ) {
      entity.cells.value[i][j].show = true;
    }
  }
  entity.cells.value[Table.startCell.row][Table.startCell.col].width =
    entity.colIndex.value[Table.startCell.col + 1].width;
  entity.cells.value[Table.startCell.row][Table.startCell.col].height =
    entity.rowIndex.value[Table.startCell.row + 1].height;
  entity.cells.value[Table.startCell.row][Table.startCell.col].rowspan = 0;
  entity.cells.value[Table.startCell.row][Table.startCell.col].colspan = 0;
};

export let TextPlace = (val) => {
  let range = GetSelectRange();
  if (!range) {
    return;
  }

  for (let i = range.startRow; i <= range.endRow; i++) {
    for (let j = range.startCol; j <= range.endCol; j++) {
      entity.cells.value[i][j].textAlign = val;
    }
  }
};

export let TextBold = () => {
  let range = GetSelectRange();
  if (!range) {
    return;
  }

  let val;
  if (entity.cells.value[range.startRow][range.startCol].bold != "600") {
    val = "600";
  } else {
    val = "normal";
  }
  for (let i = range.startRow; i <= range.endRow; i++) {
    for (let j = range.startCol; j <= range.endCol; j++) {
      entity.cells.value[i][j].bold = val;
    }
  }
};

export let TextTilt = () => {
  let range = GetSelectRange();
  if (!range) {
    return;
  }

  let val;
  if (entity.cells.value[range.startRow][range.startCol].italic != "italic") {
    val = "italic";
  } else {
    val = "normal";
  }
  for (let i = range.startRow; i <= range.endRow; i++) {
    for (let j = range.startCol; j <= range.endCol; j++) {
      entity.cells.value[i][j].italic = val;
    }
  }
};

export let TextUnderline = () => {
  let range = GetSelectRange();
  if (!range) {
    return;
  }

  let val;
  if (
    entity.cells.value[range.startRow][range.startCol].underline != "underline"
  ) {
    val = "underline";
  } else {
    val = "none";
  }
  for (let i = range.startRow; i <= range.endRow; i++) {
    for (let j = range.startCol; j <= range.endCol; j++) {
      entity.cells.value[i][j].underline = val;
    }
  }
};

export let FontSize = (val) => {
  let range = GetSelectRange();
  if (!range) {
    return;
  }

  for (let i = range.startRow; i <= range.endRow; i++) {
    for (let j = range.startCol; j <= range.endCol; j++) {
      let index = textSize.indexOf(entity.cells.value[i][j].fontSize);
      index += val;
      if (index == -1 || index == textSize.length) {
        continue;
      }
      entity.cells.value[i][j].fontSize = textSize[index];
    }
  }
};

export let TextColor = (val) => {
  let range = GetSelectRange();
  if (!range) {
    return;
  }

  for (let i = range.startRow; i <= range.endRow; i++) {
    for (let j = range.startCol; j <= range.endCol; j++) {
      entity.cells.value[i][j].color = val;
    }
  }
};

export let FillColor = (val) => {
  let range = GetSelectRange();
  if (!range) {
    return;
  }

  for (let i = range.startRow; i <= range.endRow; i++) {
    for (let j = range.startCol; j <= range.endCol; j++) {
      entity.cells.value[i][j].background = val;
    }
  }
};

export let BorderColor = () => {};

export let NoBorder = () => {};

let GetSelectRange = () => {
  let startRow, endRow, startCol, endCol;
  if (Table.startCell && Table.lastCell) {
    startRow =
      Table.startCell.row < Table.lastCell.row
        ? Table.startCell.row
        : Table.lastCell.row;
    endRow =
      Table.startCell.row > Table.lastCell.row
        ? Table.startCell.row + Table.startCell.rowspan
        : Table.lastCell.row + Table.lastCell.rowspan;

    startCol =
      Table.startCell.col < Table.lastCell.col
        ? Table.startCell.col
        : Table.lastCell.col;
    endCol =
      Table.startCell.col > Table.lastCell.col
        ? Table.startCell.col + Table.startCell.colspan
        : Table.lastCell.col + Table.lastCell.colspan;

    return { startRow, endRow, startCol, endCol };
  } else if (Table.startCell && !Table.lastCell) {
    startRow = Table.startCell.row;
    endRow = Table.startCell.row;
    startCol = Table.startCell.col;
    endCol = Table.startCell.col;

    return { startRow, endRow, startCol, endCol };
  } else {
    return null;
  }
};
